<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    protected $whitelist = array(
    		'zfcuser/login',
    		'user/login',
    );
    
    public function onBootstrap(MvcEvent $e)
    {        
       
    	$app = $e->getApplication();
    	$sm  = $app->getServiceManager();
    	
    	$nav_auth = $sm->get('BjyAuthorize\Service\Authorize');
    	$nav = $sm->get('ViewHelperManager')->get('Navigation');
    	
    	//Set Navigation Helper Menu ( Custom Layout )
    	$nav_pm = $nav->getPluginManager();
    	$nav_pm->setInvokableClass('myMenu', '\Application\View\Helper\Menu');
    	
    	//Set Navigation Resource to Show/Hide Menu
    	$nav_acl  = $nav_auth->getAcl();
    	$nav_role = $nav_auth->getIdentity();
    	$nav->setDefaultAcl($nav_acl);
    	$nav->setDefaultRole($nav_role);
    	
    	//For login service ( Redirect if not login )
    	$auth = $sm->get('zfcuser_auth_service');
    	$allowedRoutes = $this->whitelist;
    	
    	$app->getEventManager()->attach(
    			\Zend\Mvc\MvcEvent::EVENT_ROUTE,
    			function($e) use ($auth, $allowedRoutes) {
    				$app = $e->getApplication();
    				$routeMatch = $e->getRouteMatch();
    				$routeName = $routeMatch->getMatchedRouteName();
    				if (!$auth->hasIdentity()
    				&& !in_array($routeName,$allowedRoutes))
    				{
    					$response = $e->getResponse();
    					$response->getHeaders()->addHeaderLine(
    							'Location',
    							$e->getRouter()->assemble(
    									array(),
    									array('name' => 'zfcuser/login')
    							)
    					);
    					$response->setStatusCode(302);
    					return $response;
    				}
    				else
    				{
    				   
    				    return;
    				}
    			},
    			-100);
    }

    public function getConfig()
    {
        $config = array();
        $configFiles = array(
        		include __DIR__ . '/config/module.config.php',
        		include __DIR__ . '/config/navigation.config.php',
        );
        foreach ($configFiles as $file) {
        	$config = \Zend\Stdlib\ArrayUtils::merge($config, $file);
        }
        return $config;
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    
    public function initAcl(MvcEvent $e) {
    
    	$acl = new \Zend\Permissions\Acl\Acl();
    	$roles = include __DIR__ . '/config/module.acl.roles.php';
 
    	foreach ($roles as $role) {
    
    		$role = new \Zend\Permissions\Acl\Role\GenericRole($role);
    		$acl->addRole($role);
    	}
    	//testing
    	//var_dump($acl->isAllowed('admin','home'));
    	//true
    
    	//setting to view
    	$e->getViewModel()->acl = $acl;
    
    }
    
  
}
